package tech.mhuang.pacebox.core.chain;

import java.util.List;

/**
 * 责任链实现
 *
 * @author mhuang
 * @since 1.0.0
 */
public class ChainAdapter<Request,Response> implements BaseChain<Request,Response>{

    private final List<BaseInterceptor<BaseChain,Response>> interceptors;
    private final int index;
    private final Request request;

    public ChainAdapter(List<BaseInterceptor<BaseChain,Response>> interceptors, int index, Request request) {
        this.interceptors = interceptors;
        this.index = index;
        this.request = request;
    }


    @Override
    public Request request() {
        return request;
    }

    @Override
    public Response proceed(Request request) {
        ChainAdapter<Request,Response> next = new ChainAdapter(interceptors, index + 1, request);
        BaseInterceptor<BaseChain,Response> interceptor = interceptors.get(index);
        return interceptor.interceptor(next);
    }
}
